package com.lottery.material.controller.portal.menu;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.pojo.conf.MenuLevelConfig;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.menu.MenuLevelConfigService;
import com.lottery.material.util.ExceptionUtil;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.menu.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:    菜单Controller
 * Author:         sunny
 * CreateDate:     2019-04-13 13:05
 */
@RequestMapping("/portal/menu")
@Slf4j
@Controller
public class PortalMenuController extends BaseController {

    @Autowired
    private MenuLevelConfigService menuLevelConfigService;

    /**
     * description: 获取菜单模块列表
     * author sunny
     * createDate 2019-04-13 下午 1:32
     * @param request
     * @return
     */
    @RequestMapping(value = "/getMenuList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getMenuList(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }
        String menuType = reqParamMap.get("menuType");  //1：球事模块菜单；2：圈子模块菜单
        if (StringUtils.isBlank(menuType)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "menuType");
        }else if(!"1".equals(menuType) && !"2".equals(menuType)){
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_ERROR, "menuType");
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if(null != responseResult){
            return responseResult;
        }

        try {
            //根据菜单类型查询菜单列表
            List<MenuLevelConfig> menuList = menuLevelConfigService.findMenuListByLevelType(Integer.valueOf(menuType));

            //封装响应数据
            List<MenuVo> menuVos = new ArrayList<>();
            if(null != menuList && menuList.size() > 0){
                for(int i = 0; i < menuList.size(); i++){
                    MenuVo menuVo = new MenuVo();
                    MenuLevelConfig menuLevelConfig = menuList.get(i);
                    menuVo.setMenuCode(menuLevelConfig.getMenuCode());
                    menuVo.setMenuName(menuLevelConfig.getMenuName());
                    menuVos.add(menuVo);
                }
            }
            return ResponseResult.buildSuccess(menuVos);
        }catch (Exception e){
            log.error("【获取菜单模块列表出现异常】：{}", ExceptionUtil.logExceptionStack(e));
            throw new CustomException(ResultEnum.GET_MENU_LIST_EXCEPTION);
        }
    }
}
